/*
femto.css framework
"No crap, just simple HTML"

This file is part of the femto.css framework.

2020 - nokoru and contributors. no rights reserved.
This framework comes with no warranty.
femto license: Unlicense
*/
const _ = (e) => document.getElementById(e);

const toggleMobileMenu = () => {
  /* open/close mobile menu using the nav burger */
  let mobileMenu = _("femto-mobile-menu");
  if (mobileMenu !== null) {
    if (mobileMenu.style.display === "block") {
      mobileMenu.style.display = "none";
      _("femto-nav-burger").removeAttribute("class","femto-burger-active");
    } else {
      mobileMenu.style.display = "block";
      _("femto-nav-burger").setAttribute("class","femto-burger-active");
    }
  }
};

window.onload = () => {
  /* responsive nav */
  let navBurger = document.createElement("span");
  navBurger.setAttribute("id","femto-nav-burger");
  navBurger.setAttribute("onclick","toggleMobileMenu();");
  document.getElementsByTagName("nav")[0].appendChild(navBurger);

  let mobileMenu = document.createElement("ul");
  mobileMenu.setAttribute("id","femto-mobile-menu");
  document.getElementsByTagName("nav")[0].appendChild(mobileMenu);

  let listString = "";
  for (let i=0 ; i < document.getElementsByTagName("nav")[0].getElementsByTagName("a").length; i++) {
    let currentLink = document.getElementsByTagName("nav")[0].getElementsByTagName("a")[i];
    if (currentLink.getAttribute("class") === "femto-nav-logo") continue;

    listString += "<li><a href=\"" + encodeURI(currentLink.getAttribute("href")) +"\">" + currentLink.innerHTML +  "</a>";
  }
  mobileMenu.innerHTML += listString;
};

window.addEventListener("DOMContentLoaded", () => {
  document.body.onresize = () => {
    let mobileMenu = _("femto-mobile-menu");
    if (document.body.clientWidth > 800) {
      if (mobileMenu !== null) {
        mobileMenu.style.display = "none";
        _("femto-nav-burger").removeAttribute("class","femto-burger-active");
      }
    }
  };
});
